package com.aistar.service.Impl;

import com.aistar.mapper.CompanyMapper;
import com.aistar.mapper.CustomerMapper;
import com.aistar.pojo.Customer;
import com.aistar.pojo.CustomerExample;
import com.aistar.service.CustomerService;
import com.aistar.util.ServerResponse;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public ServerResponse getByTelno(Long telno) {

        CustomerExample example = new CustomerExample();
        example.createCriteria().andCustTelnoEqualTo(telno);
        List<Customer> customerList = customerMapper.selectByExample(example);
        if (customerList !=null && customerList.size()>0){//找到用户了
            Customer customer = customerList.get(0);
            return ServerResponse.hasRegist();//1,"查询数据成功",customer
        }else {
            return ServerResponse.noRegist();//0,"查询数据失败","用户不存在"
        }
    }

    //根据账号与密码查询用户
    @Override
    public ServerResponse getByAccountAndPwd(Integer type, String account, String password) {

        CustomerExample example = new CustomerExample();
        CustomerExample.Criteria criteria =  example.createCriteria();
        switch (type){
            case 0:{
                Long telno = Long.valueOf(account);
                criteria.andCustTelnoEqualTo(telno);
                break;
            }
            case 1:{
                criteria.andCustEmailEqualTo(account);
                break;
            }
            case 2:
                criteria.andCustNameEqualTo(account);
        }
        criteria.andCustPasswordEqualTo(password);
        List<Customer> customerList = customerMapper.selectByExample(example);


        if (customerList != null && customerList.size()>0)
            return ServerResponse.getDateSuccess(customerList.get(0));
        return  ServerResponse.getDataFailed("账号或密码错误，登录失败");


    }

    @Override
    public ServerResponse add(Customer customer) {

        int row = customerMapper.insert(customer);

        if (row>0)
            return ServerResponse.addSuccess();

        return ServerResponse.addFailed();
    }

    @Override
    public ServerResponse modify(Customer customer) {
        return null;
    }

    @Override
    public ServerResponse modifyStatus(Customer customer) {
        return null;
    }

    @Override
    public ServerResponse getById(Integer id) {
        return null;
    }

    @Override
    public ServerResponse getAll() {
        return null;
    }
}
